﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MemoryGames.Properties;
using System.Data;
using System.Data.SqlClient;

namespace MemoryGames.Games.WrongWord
{
	/// <summary>
	/// Interaction logic for WrongWord.xaml
	/// </summary>
	public partial class Main : UserControl, IGameControl
	{
		#region "Events"
		public event MainForm.MainFormRestartDelegate MainFormRestart;
		#endregion

		#region "Constructors"
		public Main()
		{
			InitializeComponent();
			btnBack.Click += Back_Click;
		}
		#endregion

		/// <summary>
		/// Create Q and A controls
		/// </summary>
		public bool Initialize()
		{   SqlConnection c = new SqlConnection(Settings.Default.LocalDataConnectionString);
			try
			{   //Get questions and answers from database
				c.Open();
				SqlCommand cmd = new SqlCommand("SELECT * FROM WrongWord.Question;SELECT * FROM WrongWord.Answer",c);
				SqlDataAdapter da = new SqlDataAdapter(cmd);
				DataSet ds = new DataSet();
				da.Fill(ds);
                
				//Add controls
				foreach (DataRow dr in ds.Tables[0].Rows)
				{   wp.Children.Add(new Question(dr, ds.Tables[1].Select(string.Format("QuestionId = {0}",dr["QuestionId"]))));
				}
			}
			finally
			{
				if (c.State == ConnectionState.Open)
				{   c.Close();
				}
			}

			return true;
		}
		private void Back_Click()
		{
			this.MainFormRestart.Invoke();
		}
	}
}